package com.example.news.News;

import android.content.Context;
import android.content.Intent;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com. example.news.R;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private List<News> list;
    private Context context;
    public NewsAdapter(Context context, List<News> list){
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news = list.get(position);
        holder.bigTitle.setText(news.getBigTitle());
        holder.take.setText(news.getTake());
        holder.time.setText(news.getTime());
        Glide.with(context).load(list.get(position).getImageUrl()).into(holder.imageNews);
        String mUrl = news.getUrl();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,NewsContent.class);
                intent.putExtra("url",mUrl);
                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        if(list != null){
            return list.size();
        }else {
            return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView bigTitle,take,time;
        ImageView imageNews;

        public ViewHolder(@NonNull View itemView){
            super(itemView);

            bigTitle = itemView.findViewById(R.id.bigTitle);
            take = itemView.findViewById(R.id.src);
            time = itemView.findViewById(R.id.time);
            imageNews = itemView.findViewById(R.id.imageNews);

        }
    }
}


